package sg.edu.nus.iss.club.gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListModel;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;

import sg.edu.nus.iss.club.Member;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class MemberPanel extends javax.swing.JPanel {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JList memberList;
	private JButton removeMemberButton;
	private JLabel jLabel1;
	private JButton addMemberButton;
	public ClubApplication clubApplication;
	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String [] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new MemberPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	
	public MemberPanel() {
		super();
		initGUI();
	}
	
	public Member getSelectedMember() {
		return (Member)memberList.getSelectedValue();
	}
	
	public void refresh() {
		Member[] memberListMData = clubApplication.getMembers();
		ListModel memberListModel = 
			new DefaultComboBoxModel(memberListMData);
		memberList.setModel(memberListModel);
	}
	
	public class AddMemberActionListener implements ActionListener {
		
		public void actionPerformed(ActionEvent e) {
			AddMemberDialog addMemberDialog = new AddMemberDialog(clubApplication.getMainWindow());
			addMemberDialog.clubApplication = clubApplication;
			addMemberDialog.setVisible(true);
			refresh();
		}
		
	}
	
	public class RemoveMemberActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (memberList.getSelectedValue() != null) {
				RemoveMemberDialog removeMemberDialog = new RemoveMemberDialog(clubApplication.getMainWindow());
				removeMemberDialog.setMessage("<html>Are you sure you want to remove member - " + memberList.getSelectedValue() + " ?</html>" );
				removeMemberDialog.setDeletedItem((Member)memberList.getSelectedValue());
				removeMemberDialog.clubApplication = clubApplication;
				removeMemberDialog.setVisible(true);
			}
			refresh();
		}
	}
	
	private void initGUI() {
		try {
			this.setLayout(null);
			this.setPreferredSize(new java.awt.Dimension(769, 199));
			this.setDoubleBuffered(false);
			{
				memberList = new JList();
				this.add(memberList, "0, 1, 2, 2");
				memberList.setBounds(7, 34, 648, 147);
				memberList.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
			}
			{
				addMemberButton = new JButton();
				this.add(addMemberButton, "3, 1");
				addMemberButton.setText("Add");
				addMemberButton.setBounds(667, 34, 90, 27);
				addMemberButton.addActionListener(new AddMemberActionListener());
			}
			{
				removeMemberButton = new JButton();
				this.add(removeMemberButton, "3, 2");
				removeMemberButton.setText("Remove");
				removeMemberButton.setBounds(667, 67, 90, 27);
				removeMemberButton.addActionListener(new RemoveMemberActionListener());
			}
			{
				JLabel alabel = new JLabel();
				this.add(alabel, "3, 2");
			}
			{
				jLabel1 = new JLabel();
				this.add(jLabel1);
				jLabel1.setText("Members");
				jLabel1.setBounds(6, 12, 60, 16);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
